<?php
/**
 * 汽车车型管理控制器
 *
 * @package    app
 * @version    1.0
 * @author     Ray 33705910@qq.com
 * @license    MIT License
 * @copyright  2013 - 2015 Ray
 * @link       http://wangxiaolei.cn
 */

/**
 * 本控制器主要用于：
 * 1.
 * @package  app
 * @extends  Controller
 */
namespace admin;

class Controller_Car extends Controller_BaseController {

    public function before(){
        parent::before();

        $params = array(
            'controller_name' => '车型管理'
        );
        \View::set_global($params);
    }

    /**
    * 车型列表
    * 
    **/
    public function action_index(){
        $params = array(
            'title' => '车型管理',
            'menu' => 'car'
        );
        
        $items = \Model_Car::query()
                ->where('is_delete', 0);
        if(\Input::get('cid', false)){
            $items->where('category_id', \Input::get('cid'));
        }else{
            $ids = \Model_Category::getChildIds(\Session::get('seller')->car_category);
            $items->where('category_id', 'IN', $ids);
        }

        $items->order_by('id', 'desc');
        //如果是select数据源，则不分页        
        if(\Input::is_ajax() && \Input::get('element', false) == 'select'){
            $list = $items->get();
            if( ! $list){
                die(json_encode(array('status' => 'succ', 'errcode' => 0, 'data' => array())));
            }
            
            $cars = array();
            foreach ($list as $key => $value) {
                array_push($cars, $value->to_array());
            }
            die(json_encode(array('status' => 'succ', 'errcode' => 0, 'data' => $cars)));
        }

        //分页查询
        $count = $items->count();
        $config = array(
            'pagination_url' => "/admin/car",
            'total_items'    => $count,
            'per_page'       => \Input::get('count', 10),
            'uri_segment'    => 'start',
            'show_first'     => true,
            'show_last'      => true,
            'name'           => 'bootstrap3_cn'
        );
        
        $pagination = new \Pagination($config);
        $params['pagination'] = $pagination;        
        $params['items'] = $items
                            ->rows_offset($pagination->offset)
                            ->rows_limit($pagination->per_page)
                            ->get();

        \View::set_global($params);
        $this->template->content = \View::forge('ace/car/index');
    }

    /**
    * 创建或编辑车型信息
    * 
    * 当车型ID不为0时的操作为编辑车型。反之为创建
    * @param $id 车型ID
    * 
    **/
    public function action_save($id = 0){
        $params = array(
            'title' => '车型详情',
            'menu' => 'car-details'
        );

        if($id){
            $car = \Model_Car::find($id);
        }

        if(\Input::method() == 'POST'){
            if(! \Security::check_token()){
                die('过期或无效的请求');
            }
            $data = \Input::post();
            if(isset($car) && $car){
                $car->set($data);
            }else{
                $data['seller_id'] = \Session::get('seller')->id;
                $data['is_delete'] = 0;
                $car = \Model_Car::forge($data);
                $car->features = array();
            }
            
            //关联特性文章
            if(isset($data['feature_data']) && $data['feature_data']){
                if( ! isset($car->features) || ! $car->features){
                    $car->features = array();
                }

                $items = json_decode($data['feature_data'], true);
                foreach ($items as $key => $value) {
                    if(! $value['articles']){
                        continue;
                    }
                    foreach ($value['articles'] as $node_id) {
                        $value['node_id'] = $node_id;
                        array_push($car->features, \Model_CarFeature::forge($value));
                    }
                    
                }
            }

            if(isset($data['images_data']) && $data['images_data']){
                if( ! isset($car->gallery) || ! $car->gallery){
                    $car->gallery = array();
                }

                $items = json_decode($data['images_data'], true);
                foreach ($items as $key => $value) {
                    array_push($car->gallery, \Model_CarGallery::forge($value));
                }
            }

            if($car->save()){
                \Session::set_flash('msg', array('status' => 'succ', 'msg' => '操作成功', 'errcode' => 0)); 
            }else{
                \Session::set_flash('msg', array('status' => 'err', 'msg' => '操作失败', 'errcode' => 20)); 
            }
        }

        if(isset($car) && $car){
            $params['item'] = \Model_Car::find($id);
        }

        \View::set_global($params);
        $this->template->content = \View::forge('ace/car/details');
    }

    /**
    * 车型系列列表
    **/
    public function action_category(){
        $params = array(
            'title' => '车型系统管理',
            'menu' => 'car-category'
        );

        $items = \Model_Car::query();
        $items->where('seller_id', \Session::get('seller')->id);

        //分页查询
        $count = $items->count();
        $config = array(
            'pagination_url' => "/admin/category",
            'total_items'    => $count,
            'per_page'       => \Input::get('count', 10),
            'uri_segment'    => 'start',
            'show_first'     => true,
            'show_last'      => true,
            'name'           => 'bootstrap3_cn'
        );
        
        $pagination = new \Pagination($config);
        $params['pagination'] = $pagination;        
        $params['items'] = $items
                            ->rows_offset($pagination->offset)
                            ->rows_limit($pagination->per_page)
                            ->get();
        $params['levels'] = \Model_MemberLevel::query()->get();

        \View::set_global($params);
        $this->template->content = \View::forge('admin/default/car/series');
    }

    /**
    * 车型系列列表
    **/
    public function action_feature(){
        $params = array(
            'title' => '车型系统管理',
            'menu' => 'car-category'
        );

        if(\Input::is_ajax()){
            $data = \Input::get();
            if($data['action'] == 'delete'){
                if(! isset($data['id']) || ! $data['id']){
                    die(json_encode(array('status' => 'err', 'msg' => '无效的参数', 'errcode' => 10)));
                }
                $feature = \Model_CarFeature::find($data['id']);
                if($feature->delete()){
                    die(json_encode(array('status' => 'succ', 'msg' => '操作成功', 'errcode' => 0)));
                }else{
                    die(json_encode(array('status' => 'err', 'msg' => '操作成失败', 'errcode' => 20)));
                }
            }
        }

        $items = \Model_Car::query();
        $items->where('seller_id', \Session::get('seller')->id);

        //分页查询
        $count = $items->count();
        $config = array(
            'pagination_url' => "/web/admin/article",
            'total_items'    => $count,
            'per_page'       => \Input::get('count', 10),
            'uri_segment'    => 'start',
            'show_first'     => true,
            'show_last'      => true,
            'name'           => 'bootstrap3_cn'
        );
        
        $pagination = new \Pagination($config);
        $params['pagination'] = $pagination;        
        $params['items'] = $items
                            ->rows_offset($pagination->offset)
                            ->rows_limit($pagination->per_page)
                            ->get();
        $params['levels'] = \Model_MemberLevel::query()->get();

        \View::set_global($params);
        $this->template->content = \View::forge('admin/default/car/series');
    }

    /**
    * 删除一条车型信息
    *
    * @param $id int 车型信息ID
    * @return
    */
    public function action_delete($id){
        if( ! $id){
            die('无效的参数');
        }

        $node = \Model_Car::find($id);
        if( ! $node){
            die('无效的参数');
        }

        $node->is_delete = 1;
        if($node->save()){
            if(\Input::is_ajax()){
                die(json_encode(array('stauts' => 'succ', 'msg' => '操作成功', 'errcode' => 0)));
            }
            \Response::redirect("/admin/car/");
        }
        if(\Input::is_ajax()){
            die(json_encode(array('stauts' => 'err', 'msg' => '操作失败', 'errcode' => 20)));
        }
    }

}
